<template>
  <div class="search-components">
    <div class="search-component">
      <div class="row keyword">
        <p class="label">关键词</p>
        <el-input
          v-model="searchModel.code"
          style="width: 220px"
          placeholder="泊位编号"
          @keyup.enter="handleSearch"
        />
      </div>
      <div class="row keyword">
        <p class="label">所属行政区</p>
        <el-cascader
          v-model="searchModel.regionId"
          :options="regionId.data"
          filterable
          style="width: 220px"
          placeholder="所属行政区"
          :props="{
            value: 'id',
            label: 'name',
            children: 'nodes',
          }"
          @change="handleCascaderChange"
          clearable
        />
      </div>
      <div class="row">
        <p class="label">所属路段</p>
        <el-select
          v-model="searchModel.sectionId"
          filterable
          style="width: 220px"
          placeholder="所属路段"
        >
          <el-option
            v-for="item in sectionId.data"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>
      </div>
      <div class="search-btn">
        <el-button class="reset-btn" type="primary" @click="handleReset">
          <img class="icon-img" :src="iconReset" alt="" />
          重置</el-button
        >
        <el-button class="submit-btn" type="primary" @click="handleSearch">
          <img class="icon-img" :src="iconSearch" alt="" />
          查询</el-button
        >
      </div>
    </div>
  </div>
</template>

<script setup>
import { reactive, ref } from "vue";
import iconSearch from "@/assets/image/btn/icon-search.png";
import iconReset from "@/assets/image/btn/icon-reset.png";
const emits = defineEmits(["search"]);
import { getCanton, getRoadList } from "@/service/modules/publicModule";

const searchModel = ref({
  code: "",
  regionId: "",
  sectionId: "",
});

const regionId = reactive({
  data: [
    {
      id: "",
      name: "全部",
    },
  ],
});

const sectionId = reactive({
  data: [
    {
      id: "",
      name: "全部",
    },
  ],
});

const handleCascaderChange = async (value) => {
  console.log(value);
  if (value !== null && value.length > 0) {
    searchModel.value.regionId = value[value.length - 1];
  } else {
    searchModel.value.regionId = "";
  }
  await getRoadList(searchModel.value.regionId).then((res) => {
    if (Array.isArray(res.data.result)) {
      // 检查数组中是否已存在"全部"选项，如果不存在，则添加
      if (res.data.result[0].name !== "全部") {
        res.data.result.unshift(sectionId.data[0]);
      }
      // 更新响应式数据
      sectionId.data = res.data.result;
    }
  });
};

const getData = async () => {
  await getCanton().then((res) => {
    if (Array.isArray(res.data.result)) {
      // 检查数组中是否已存在"全部"选项，如果不存在，则添加
      if (res.data.result[0].name !== "全部") {
        res.data.result.unshift(regionId.data[0]);
      }
      // 更新响应式数据
      regionId.data = res.data.result;
    }
  });
  await getRoadList(searchModel.value.regionId).then((res) => {
    if (Array.isArray(res.data.result)) {
      // 检查数组中是否已存在"全部"选项，如果不存在，则添加
      if (res.data.result[0].name !== "全部") {
        res.data.result.unshift(sectionId.data[0]);
      }
      // 更新响应式数据
      sectionId.data = res.data.result;
    }
  });
};
getData();

const handleReset = async () => {
  searchModel.value = {};
  // await getRoadList().then((res) => {
  //   sectionId.data = res.data.result;
  // });
  emits("search", searchModel.value);
};

const handleSearch = () => {
  // console.log(`output-> searchModel.value`, searchModel.value);
  emits("search", searchModel.value);
};
</script>

<style lang="less" scoped>
.search-components {
  .search-component {
    display: flex;
    flex-wrap: wrap;
    .row {
      display: flex;
      align-items: center;
      margin-bottom: 16px;
    }
    .label {
      font-size: 14px;
      color: #333333;
      margin-right: 8px;
      font-weight: 400;
    }
    .keyword {
      margin-right: 32px;
    }
  }
  .search-btn {
    margin-left: 50px;
    display: flex;
    .reset-btn {
      background: #f2f3f5;
      border: 1px solid #f2f3f5;
      border-radius: 2px;
      color: #333333;
      padding: 5px 16px;
    }
    .submit-btn {
      background: #007aff;
      border: 1px solid #007aff;
      border-radius: 2px;
      padding: 5px 16px;
    }
    .icon-img {
      width: 16px;
      height: 16px;
      margin-right: 8px;
    }
  }
}
</style>